<?php

namespace Biz;

use Base\Exception\NoRecordException;

class Customer extends \Base\BaseBiz
{
    /**
     * 创建用户
     * @throws \Exception
     */
    public function create($name, $telephone, $login_account, $password, $province_id, $city_id, $area_id, $address,
                           $wechat_account, $identity_card_number, $status, $portrait, $admin_remark='')
    {
        try {
            $transaction = new \Base\BaseOrmModel();
            $transaction->beginTransaction();
            // 判断用户登录账号是否已经存在
            \Biz\Customer\Customer::checkLoginAccount($login_account);
            // 插入客户基本信息
            $customer_id = \Biz\Customer\Customer::insert($name, $telephone, $login_account, $password, $province_id, $city_id, $area_id,$address, $wechat_account, $identity_card_number, $portrait, $status, $admin_remark);
            // 获取用户信息
            $customer_orm = new \Orm\Customer\CustomerClass();
            $customer_orm->loadById($customer_id);
            $transaction->commit();
            return $customer_id;
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    public function update($customer_id, $name, $telephone, $login_account, $password, $province_id, $city_id, $area_id, $address,
                           $wechat_account, $identity_card_number, $rank_id, $status, $portrait, $admin_remark)
    {
        try {
            $transaction = new \Base\BaseOrmModel();
            // 获取用户信息
            $customer_orm = new \Orm\Customer\CustomerClass();
            $customer_orm->loadById($customer_id);

            // 判断用户登录账号是否已经存在
            \Biz\Customer\Customer::checkLoginAccount($login_account, TRUE);
            // 更新客户基本信息
            $re = \Biz\Customer\Customer::update($customer_id, $name, $telephone, $login_account, $password, $province_id, $city_id, $area_id,
                $address, $wechat_account, $identity_card_number, $portrait, $rank_id, $status, $admin_remark);
            $transaction->commit();
            return $re;
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 审核通过
     * @param $customer_id
     * @param $status
     */
    public function updateStatus($customer_id, $status)
    {
        switch ($status) {
            case 'check': // 审核通过
                self::checkCustomer($customer_id);
                break;
            case 'stop': // 停用
                $customer_orm = new \Orm\Customer\CustomerClass();
                $customer_orm->loadById($customer_id);
                $customer_orm['status'] = \Orm\Customer\CustomerClass::STATUS_DISABLE;
                $customer_orm->update();
                break;
            case 'open':
                $customer_orm = new \Orm\Customer\CustomerClass();
                $customer_orm->loadById($customer_id);
                $customer_orm['status'] = \Orm\Customer\CustomerClass::STATUS_NORMAL;
                $customer_orm->update();
                break;

        }
    }

    /**
     * 审核通过客户
     * @param $customer_id
     * @throws \Exception
     */
    public function checkCustomer($customer_id)
    {
        try {
            $transaction = new \Base\BaseOrmModel();
            $transaction->beginTransaction();
            try {
                $customer_orm = new \Orm\Customer\CustomerClass();
                $customer_orm->loadById($customer_id);
                if ($customer_orm['status'] != \Orm\Customer\CustomerClass::STATUS_NO_CHECK) {
                    throw new \Exception($customer_orm['name'] . '已审核通过，不可重复审核');
                }
            } catch (NoRecordException $e) {
                throw new \Exception('客户信息不存在', __LINE__);
            }
            $customer_orm['status'] = \Orm\Customer\CustomerClass::STATUS_NORMAL;
            $customer_orm['check_time'] = date('Y-m-d H:i:s', time()); // 更新用户审核时间
            $customer_orm->update();
            if ($customer_orm['type'] == \Orm\Customer\CustomerClass::TYPE_DISTRIBUTE) {
                // 发放客户 货物 货款(改在财务部分)
                \Biz\Bonus\Bonus::configCustomer($customer_id);
                // 一次性推荐奖励
                \Biz\Bonus\Bonus::recommendAdministratePrice($customer_id);
            }
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    public function getCustomerList($where, $now, $size)
    {
        // 获取客户列表
        return \Biz\Customer\Customer::getCustomerList($where, $now, $size);
    }

    public function searchCustomer($where, $now, $size){
        // 获取客户列表
        return \Biz\Customer\Customer::searchCustomer($where, $now, $size);
    }


    public function getById($id)
    {
        $customer_orm = new \Orm\Customer\CustomerClass();
        $customer_orm->loadById($id);
        return $customer_orm;
    }

    /**
     * 重置客户密码
     * @param $customer_id
     * @throws \Exception
     */
    public function resetPassword($customer_id)
    {
        try {
            $customer_orm = new \Orm\Customer\CustomerClass();
            $customer_orm->loadById($customer_id);
            $customer_orm['password'] = md5('123456');
            $customer_orm->update();
        } catch (NoRecordException $e) {
            throw new \Exception('用户不存在', __LINE__);
        }
    }

    /**
     * 停用客户
     * @param $customer_id
     * @throws \Exception
     */
    public function stopCustomer($customer_id)
    {
        try {
            $customer_orm = new \Orm\Customer\CustomerClass();
            $customer_orm->loadById($customer_id);
            $customer_orm['status'] = \Orm\Customer\CustomerClass::STATUS_DISABLE;
            $customer_orm->update();
        } catch (NoRecordException $e) {
            throw new \Exception('用户不存在', __LINE__);
        }
    }

    /**
     * 删除客户
     * @param $customer_id
     * @throws \Exception
     */
    public function deleteCustomer($customer_id)
    {
        \Biz\Customer\Customer::delete($customer_id);
    }

    /**
     * 拒绝客户
     * @param $customer_id
     * @param $admin_remark
     */
    public function refuseCustomer($customer_id, $admin_remark)
    {
        \Biz\Customer\Customer::refuse($customer_id, $admin_remark);
    }


    /*
     * 根据open_id获取客户信息
     * */
    public function getByOpenId($open_id)
    {
        if($open_id){
            $customer_orm = new \Orm\Customer\CustomerModel();
            $customer_openid = $customer_orm->loadByOpenId($open_id);
        }
        return $customer_openid;
    }
}